package cn.lxycx.dataterrace.controller;


import com.jfinal.kit.PropKit;
import cn.lxycx.dataterrace.interceptor.validates.AccountValidate;

import java.util.Map;

import org.apache.log4j.Logger;

import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * 账号管理
 * @author XingChao
 * @version [V1.0 , 2019年4月24日]
 */
public class AccountController extends Controller{
	private Logger log = Logger.getLogger(AccountController.class);
	private String driver = PropKit.get("driver");

	
	@ActionKey("/accountList")
	public void accountList(){
		renderJsp("accountList.jsp");
	}
	
	
	/**
	 * 
	 * 查询账号
	 * @author 邢超
	 * 创建时间：2017年9月22日
	 *
	 */
	public void findList(){
		Integer page = this.getParaToInt("page");
		Integer limit = this.getParaToInt("limit");
		//List<Record> namelist = Db.find();
		Record user = this.getSessionAttr("user");
		String sql = "select * from data_statement_account where role>? or id=?";
		Page<Record> list = Db.paginate(page, limit, "select *","from ("+sql+") ttt",user.get("role"),user.get("id"));
		
		this.setAttr("code", 0);
		this.setAttr("msg", "查询成功");
		this.setAttr("count", list.getTotalRow());
		this.setAttr("list", list.getList());
		renderJson();
	}
	
	/**
	 * 保存操作
	 * @author XingChao
	 */
	@Before(AccountValidate.class)
	public void save() {
		Map<String,Object> param = this.getAttr("param");
		boolean flag = false;
		if(param.containsKey("id")) {
			//更新
			flag = Db.update("data_statement_account","id",new Record().setColumns(param));
		}else {
			//新增
			try {
				Record record = new Record().setColumns(param);

				if(driver.indexOf("oracle")>0){
					record.set("id", "data_statement_account_seq.nextval");
				}

				flag = Db.save("data_statement_account",record);


			} catch (Exception e) {
				this.setAttr("res", -1);
				this.setAttr("msg", "该账号已存在");
				renderJson();
				return;
			}
		}
		
		if(flag) {
			this.setAttr("res", 1);
			this.setAttr("msg", "保存成功");			
		}else {
			this.setAttr("res", -1);
			this.setAttr("msg", "保存失败");
		}
		
		renderJson();
	}
	
	
	//修改状态
	public void upstatus() {
		Integer id = this.getParaToInt("id");
		Integer status = this.getParaToInt("status");
		if(id!=null&&status!=null) {
			boolean b = Db.update("data_statement_account", "id", new Record().set("id", id).set("status", status));
			if(b) {
				this.setAttr("res", 1);
				this.setAttr("msg", "操作成功");
			}else {
				this.setAttr("res", -1);
				this.setAttr("msg", "操作失败");
			}
		}else {
			this.setAttr("res", -1);
			this.setAttr("msg", "缺少必要参数");
		}
		renderJson();
	}
	

	
	
	
}
